package com.bear.cmcc.NetTestHTTP.ui.activity;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.NetTestCellInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestRoundInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestTimesInfoBean;
import com.bear.cmcc.NetTestHTTP.constant.MyCommonConstant;
import com.bear.cmcc.NetTestHTTP.constant.NetTestDBConstant;
import com.bear.cmcc.NetTestHTTP.ui.view.PullToRefreshView;
import com.bear.cmcc.NetTestHTTP.ui.view.PullToRefreshView.OnFooterRefreshListener;
import com.bear.cmcc.NetTestHTTP.ui.view.PullToRefreshView.OnHeaderRefreshListener;
import com.bear.cmcc.NetTestHTTP.util.MyCommonUtil;
import com.bear.cmcc.NetTestHTTP.util.NetTestUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * 轮的详情界面
 * 
 *
 */
public class TheTestDetailActivity extends AbstractActivity implements OnHeaderRefreshListener, OnFooterRefreshListener {
	PullToRefreshView mPullToRefreshView;

	private TextView netType = null;

	/**
	 * 测试开始时间
	 */
	private TextView time = null;

	/**
	 * 测试结束时间
	 */
	private TextView time_end = null;

	private TextView max_speed = null;

	private TextView min_speed = null;

	private TextView average_speed = null;
	
	private TextView up_speed = null;
	
//	private TextView down_speed = null;

	/**
	 * 测试次数
	 */
	private TextView tv_test_number_times = null;
    /**
     * 抖动强度
     */
    private TextView shake = null;

	/**
	 * 开始小区
	 */
	private TextView tv_start_cell = null;

	private TextView tv_end_cell = null;

	private MyHander myHander = new MyHander();

	private int pid = 0;
	
	private int bottomIdx = 0;

	private String gap = " : ";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		loadBottomBar();
		addGoBackButton();
		setTitle(R.string.the_test_detail);
		setContentView(R.layout.activity_the_detail);

		mPullToRefreshView = (PullToRefreshView) findViewById(R.id.main_pull_refresh_view);
		mPullToRefreshView.setOnHeaderRefreshListener(this);
		mPullToRefreshView.setOnFooterRefreshListener(this);

		initializeView();
		if (getIntent() != null) {
			pid = getIntent().getIntExtra(NetTestDBConstant.TestRoundFiledName.pid, 0);
			bottomIdx = getIntent().getIntExtra(MyCommonConstant.BottomIdxCode.bottom_dis, 0);
		}
		choseBottomItemIndex(bottomIdx);

		// 加载数据。
		myHander.sendEmptyMessage(refresh_data);

	}
	
    @Override
    protected void onStart()
    {
    	choseBottomItemIndex(bottomIdx);
    	super.onStart();
    }

	@Override
	protected void onDestroy() {
		if (myHander != null) {
			myHander.removeMessages(refresh_data);
			myHander = null;
		}
		super.onDestroy();
	}

	/**
	 * 初始化显示控件。
	 */
	private void initializeView() {
		netType = (TextView) findViewById(R.id.tv_detail_netType_value);
		time = (TextView) findViewById(R.id.tv_detail_time_value);
		time_end = (TextView) findViewById(R.id.tv_detail_time_value_end);
		max_speed = (TextView) findViewById(R.id.tv_detail_collect_max_value);
		min_speed = (TextView) findViewById(R.id.tv_detail_collect_min_value);
		average_speed = (TextView) findViewById(R.id.tv_detail_collect_down_value);
		up_speed= (TextView) findViewById(R.id.tv_detail_collect_ave_value);
//		down_speed = (TextView) findViewById(R.id.tv_detail_collect_down_value); 
		tv_test_number_times = (TextView) findViewById(R.id.tv_test_number_times);
		tv_start_cell = (TextView) findViewById(R.id.tv_start_cell);
		tv_end_cell = (TextView) findViewById(R.id.tv_end_cell);
        shake = (TextView) findViewById(R.id.tv_detail_collect_wav_value);
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		mPullToRefreshView.postDelayed(new Runnable() {
			public void run() {
				myHander.sendEmptyMessage(refresh_data_dowm);
				mPullToRefreshView.onFooterRefreshComplete();
			}
		}, 1000);
	}

	public void onHeaderRefresh(PullToRefreshView view) {
		mPullToRefreshView.postDelayed(new Runnable() {
			public void run() {
				myHander.sendEmptyMessage(refresh_data_up);
				mPullToRefreshView.onHeaderRefreshComplete();

			}
		}, 1000);

	}

	/**
	 * 刷新数据。
	 */
	private void refreshData() {
		try {
			if (pid > 0) {
				NetTestRoundInfoBean roundInfo = NetTestUtil.getNetTestRoundsInfosBypid(this, pid);
					ArrayList<NetTestCellInfoBean> listTci = NetTestUtil.getNetTestCellInfosByPid(this,
							pid);
				if (roundInfo != null) {

					String allNetType = getAllNetType(roundInfo);

					netType.setText(gap + allNetType);
					/**
					 * 测试开始时间
					 */
					time.setText(gap + MyCommonUtil.dateFormatTo(roundInfo.getStartTime()));
					time_end.setText(gap + MyCommonUtil.dateFormatTo(roundInfo.getEndTime()));

					max_speed.setText(gap + MyCommonUtil.getFormatString(roundInfo.getMaxSpeed()));
					min_speed.setText(gap + MyCommonUtil.getFormatString(roundInfo.getMinSpeed()));
					average_speed.setText(gap + MyCommonUtil.getFormatString(roundInfo.getAverageSpeed()));
					up_speed.setText(gap + MyCommonUtil.getFormatString(roundInfo.getUp_averageSpeed()));

					if (listTci != null) {
						tv_test_number_times.setText(gap + listTci.size());
						tv_start_cell.setText(gap + listTci.get(0).getCellName());
						tv_end_cell.setText(gap + listTci.get(listTci.size() - 1).getCellName());

					}
					shake.setText(gap + NetTestUtil.getWaveShakeStrong(this,pid));
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取前一轮数据(比较老的数据)。
	 */
	private void getBeforeData() {
		try {
			ArrayList<NetTestRoundInfoBean> beans = NetTestUtil.getNetTestRoundsInfos(this, false);
			if (beans == null || beans.size() == 1) {
				MyCommonUtil.showMessage(this, R.string.not_up_round_data);
				return;
			}
			// 获取最接近pid的值，并且是小于pid
			int minPid = 0;
			for (NetTestRoundInfoBean element : beans) {
				if (element.getPID() < pid) {
					if (element.getPID() > minPid) {
						minPid = element.getPID();
					}
				}
			}

			if (minPid == pid || pid == 0 || minPid == 0) {
				MyCommonUtil.showMessage(this, R.string.not_up_round_data);
				return;
			}

			pid = minPid;

			myHander.sendEmptyMessage(refresh_data);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 获取下一轮数据。
	 */
	private void getNextData() {
		try {
			ArrayList<NetTestRoundInfoBean> beans = NetTestUtil.getNetTestRoundsInfos(this, false);
			if (beans == null || beans.size() == 1) {
				MyCommonUtil.showMessage(this, R.string.not_down_round_data);
				return;
			}
			// 获取最接近pid的值，并且是大于pid
			int minPid = 0;
			for (NetTestRoundInfoBean element : beans) {
				if (element.getPID() > pid) {
					if (minPid == 0) {
						minPid = element.getPID();
					}

					if (element.getPID() < minPid) {
						minPid = element.getPID();
					}
				}
			}

			if (minPid == pid || (pid == 0) || minPid == 0) {
				MyCommonUtil.showMessage(this, R.string.not_down_round_data);
				return;
			}

			pid = minPid;

			myHander.sendEmptyMessage(refresh_data);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取次中的网络类型（去掉重复的）。
	 * 
	 * @param roundInfo
	 * @return
	 */
	private String getAllNetType(NetTestRoundInfoBean roundInfo) {
		String allNetType = "";
		try {
			ArrayList<NetTestTimesInfoBean> times = NetTestUtil.getNetTestTimesInfoByRound(this, roundInfo.getPID());
			if (times != null && times.size() > 0) {
				// 先读取一天数据。
				allNetType = times.remove(0).getNetType();
				for (NetTestTimesInfoBean element : times) {
					if (!allNetType.contains(element.getNetType())) {
						if (element.getNetType() != null) {
							allNetType = allNetType + ";" + element.getNetType();
						}
					}

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return allNetType;
	}

	private final int refresh_data_dowm = 3;

	private final int refresh_data = 2;

	private final int refresh_data_up = 1;

	class MyHander extends Handler {
		@Override
		public void handleMessage(Message msg) {
			if (msg != null) {
				switch (msg.what) {
					case refresh_data:
						refreshData();
						break;
					case refresh_data_dowm:
						getBeforeData();
						break;
					case refresh_data_up:
						getNextData();
						break;
				}
			}
		}
	}
}
